﻿using System.Collections;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace Gmantis.Controls.Toolbar
{
    [ContentProperty("Groups")]
    public class ToolbarTabItem : TabItem
    {
        // Fields
        public static readonly DependencyProperty GroupItemsSourceProperty = DependencyProperty.Register("GroupItemsSource", typeof(IEnumerable), typeof(ToolbarTabItem), new PropertyMetadata(null, new PropertyChangedCallback(ToolbarTabItem.OnGroupsSourcePropertyChanged)));
        public static readonly DependencyProperty GroupsProperty = DependencyProperty.Register("Groups", typeof(ToolbarGroupCollection), typeof(TabItem), new PropertyMetadata(null));
        private ToolbarPanel pnl = new ToolbarPanel();

        // Methods
        public ToolbarTabItem()
        {
            base.DefaultStyleKey = typeof(ToolbarTabItem);
            base.SetValue(GroupsProperty, new ToolbarGroupCollection());
            this.Groups.CollectionChanged += new NotifyCollectionChangedEventHandler(this.Groups_CollectionChanged);
            base.Content = this.pnl;
        }

        private void Groups_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            this.pnl.Children.Clear();
            foreach (ToolbarGroup group in this.Groups)
            {
                group.SetBinding<ToolbarTabItem>(Control.FontFamilyProperty, this, tb => tb.FontFamily);
                group.SetBinding<ToolbarTabItem>(Control.FontSizeProperty, this, tb => tb.FontSize);
                group.SetBinding<ToolbarTabItem>(Control.FontStretchProperty, this, tb => tb.FontStretch);
                group.SetBinding<ToolbarTabItem>(Control.FontStyleProperty, this, tb => tb.FontStyle);
                group.SetBinding<ToolbarTabItem>(Control.FontWeightProperty, this, tb => tb.FontWeight);
                group.SetBinding<ToolbarTabItem>(Control.BackgroundProperty, this, tb => tb.Background);
                group.SetBinding<ToolbarTabItem>(Control.BorderBrushProperty, this, tb => tb.BorderBrush);
                group.SetBinding<ToolbarTabItem>(ToolbarGroup.PressedBrushProperty, this, tb => tb.PressedBrush);
                group.SetBinding<ToolbarTabItem>(ToolbarGroup.MouseOverBrushProperty, this, tb => tb.MouseOverBrush);
                Toolbar source = this.FindParent<Toolbar>();
                if (source != null)
                {
                    group.SetBinding<Toolbar>(ToolbarGroup.FocusBrushProperty, source, tb => tb.FocusBrush);
                }
                this.pnl.Children.Add(group);
            }
        }

        private static void OnGroupsSourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            ToolbarTabItem item = (ToolbarTabItem)obj;
            if (item.GroupItemsSource != null)
            {
                item.Groups.Clear();
                foreach (object obj2 in item.GroupItemsSource)
                {
                    ToolbarGroup group = new ToolbarGroup
                    {
                        DataContext = obj2
                    };
                    item.Groups.Add(group);
                }
            }
        }

        // Properties
        public IEnumerable GroupItemsSource
        {
            get { return (IEnumerable)GetValue(GroupItemsSourceProperty); }
            set { SetValue(GroupItemsSourceProperty, value); }
        }

        public ToolbarGroupCollection Groups
        {
            get { return (ToolbarGroupCollection)GetValue(GroupsProperty); }
        }
    }
}
